from django.shortcuts import render
from rest_framework.response import Response
from rest_framework.views import APIView
from rest_framework.permissions import IsAuthenticated
from coupon.services import get_user_coupon_list, get_user_enable_coupon_list
from fuguang.utils import constants


class CouponListView(APIView):
    permission_classes = [IsAuthenticated, ]

    def get(self, request):
        user_id = request.user.id
        coupon_list = get_user_coupon_list(user_id)
        return Response({'message': '优惠劵列表获取成功', 'coupon': coupon_list})


class EnableCouponListView(APIView):
    permission_classes = [IsAuthenticated, ]

    def get(self, request):
        user_id = request.user.id
        data = get_user_enable_coupon_list(user_id)
        return Response({
            'message': '可用优惠劵查询成功',
            'coupon_list': data,
            # 用户可用积分
            'has_credit': request.user.credit,
            # 积分兑换比例  常量
            'credit_to_money': constants.CREDIT_TO_MONEY
        })
